package com.hsy.baoinbao.application;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.deparse.dpframe.ui.application.DPBaseApplication;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.baoinbao.common.Consts;
import com.hsy.baoinbao.modules.proguard.DemoReceiverProguard;
import com.hsy.baoinbao.modules.proguard.DemoServiceProguard;
import com.hsy.baoinbao.utils.CommonUtils;
import com.hsy.baoinbao.utils.HttpUtil;
import com.marswin89.marsdaemon.DaemonClient;
import com.marswin89.marsdaemon.DaemonConfigurations;
import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushConfig;
import com.tencent.android.tpush.XGPushManager;
import com.tencent.android.tpush.XGPushReceiver;
import com.tencent.android.tpush.service.XGPushService;

import java.util.List;


/**
 * Created by hsy on 2016/3/23.
 */
public class BIBApplication extends DPBaseApplication {

    DaemonClient client;

//    @Override
//    protected void attachBaseContext(Context base) {
//        super.attachBaseContext(base);
//        client = new DaemonClient(getConfig());
//        client.onAttachBaseContext(base);
//    }

    private DaemonConfigurations getConfig() {
        DaemonConfigurations.DaemonConfiguration configuration1 = new DaemonConfigurations.DaemonConfiguration(
                "com.hsy.baoinbao:xg_service_v2",
                XGPushService.class.getCanonicalName(),
                XGPushReceiver.class.getCanonicalName()
        );
        DaemonConfigurations.DaemonConfiguration configuration2 = new DaemonConfigurations.DaemonConfiguration(
                "com.hsy.baoinbao:proguard",
                DemoServiceProguard.class.getCanonicalName(),
                DemoReceiverProguard.class.getCanonicalName()
        );
        DaemonConfigurations.DaemonListener listener = new DaemonConfigurations.DaemonListener() {
            @Override
            public void onPersistentStart(Context context) {
                Log.d("yyy", "DaemonListener.onPersistentStart");
            }

            @Override
            public void onDaemonAssistantStart(Context context) {
                Log.d("yyy", "DaemonListener.onPersistentStart");
            }

            @Override
            public void onWatchDaemonDaed() {
                Log.d("yyy", "DaemonListener.onPersistentStart");
            }
        };
        return new DaemonConfigurations(configuration1, configuration2, listener);
    }

    private static BIBApplication mInstance = null;

    private XGIOperateCallback xgiOperateCallback = new XGIOperateCallback() {
        @Override
        public void onSuccess(Object data, int code) {
            DPLogUtil.d("XGIOperateCallback registerPush onSuccess " + String.format("token=%s code=%d", data, code));
        }

        @Override
        public void onFail(Object data, int code, String msg) {
            DPLogUtil.d("XGIOperateCallback registerPush onFail " + String.format("token=%s code=%d msg=%s", data, code, msg));
        }
    };

    public void exit(){
        Object obj = null;
        try {
            obj.notify();
        } catch (Exception e){
            //do nothing
        }
    }

    public void registerXGPushService() {
        DPLogUtil.d("registerXGPushService uid=" + CommonUtils.getUid(getApplicationContext()));
        XGPushConfig.enableDebug(getApplicationContext(), false);

        XGPushManager.enableService(getApplicationContext(), true);
        XGPushManager.startPushService(getApplicationContext());
        XGPushManager.registerPush(getApplicationContext(), CommonUtils.getUid(getApplicationContext()), xgiOperateCallback);
    }

    public void unregisterXGPushService() {
        DPLogUtil.d("unregisterXGPushService uid=" + CommonUtils.getUid(getApplicationContext()));
        XGPushManager.unregisterPush(getApplicationContext(), xgiOperateCallback);
    }

    public String getProcessName(Context cxt) {
        int myPid = android.os.Process.myPid();
        ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
            if (procInfo.pid == myPid) {
                return procInfo.processName;
            }
        }
        return null;
    }

    public static BIBApplication getInstance(){
        return mInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        String processName = getProcessName(getApplicationContext());
        DPLogUtil.d("UGarageApplication.onCreate() processName=" + processName);
        if (Consts.PROCESS_NAME_BIB.equals(processName)) {
            mInstance = this;
            HttpUtil.init(getApplicationContext());
        }
    }
}
